﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_News_Detail
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Page.RouteData.Values("Id") Is Nothing Then
                hidId.Value = Page.RouteData.Values("Id")
                LoadData()
            End If
        End If
    End Sub
    Private Sub LoadData()
        Using Data As New VSEntities
            Dim p = (From q In Data.Contents Where q.Id = hidId.Value And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending Select New With {q.Title, q.QueryString, q.Id, q.Image, q.MenuId, .MenuName = q.Menu.Title, q.Description, q.Contents, q.Author, q.PublishedDate}).FirstOrDefault
            If Not p Is Nothing Then

                Page.Title = p.Title
                Page.MetaDescription = p.Description

                ltrTitle.Text = "<h1>" & p.Title & "</h1>"
                ltrDes.Text = "<h2>" & p.Description & "</h2>"
                ltrContent.Text = p.Contents
                ltrAuthor.Text = p.Author
                lblDate.Text = DateToString(p.PublishedDate, "mm/dd/yyyy")


                'Show keyword
                Dim strKeyWord As String = ""
                Dim obj = (From ck In Data.ContentsKeywords Join k In Data.Keywords On ck.KeywordId Equals k.KeywordID Where ck.ContentId = p.Id Order By k.Keyword1 Ascending Select New With {.KeyId = ck.KeywordId, .KeyWord = k.Keyword1, .QueryString = k.QueryString}).ToList

                For i As Integer = 0 To obj.Count - 1
                    strKeyWord &= obj(i).KeyWord & ", "
                Next
                Page.MetaKeywords = strKeyWord

                'New relation
                'Lay toan bo cap con cua Menu
                hidMenuId.Value = p.MenuId
                BindToGrid(1)

            End If
        End Using
    End Sub
    Protected Sub BindToGrid(ByVal iPage As Integer)
        Using Data As New VSEntities
            Dim a = (From r In Data.uspSelectSubMenu(hidMenuId.Value) Select r.MenuId).ToArray

            Dim h = (From q In Data.Contents Where q.Id <> hidId.Value And (q.MenuId = hidMenuId.Value Or a.Contains(hidMenuId.Value)) And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending Select New With {q.Title, q.QueryString, q.Id, q.PublishedDate}).Skip((iPage - 1) * 10).Take(10).ToList
            Dim count As Integer = (From q In Data.Contents Where q.Id <> hidId.Value And (q.MenuId = hidMenuId.Value Or a.Contains(hidMenuId.Value)) And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending Select New With {q.Title, q.QueryString, q.Id, q.PublishedDate}).Count

            'Phan trang
            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, iPage, 10, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            rptRelation.DataSource = h
            rptRelation.DataBind()
        End Using

    End Sub



    Protected Sub rptRelation_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptRelation.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim lblDate As Label = CType(e.Item.FindControl("lblDate"), Label)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "chi-tiet/" & row.QueryString & "-" & row.Id.ToString & ".html"
        lblDate.Text = DateToString(row.PublishedDate, "mm/dd/yyyy")
    End Sub


    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub

End Class
